package com.tuanzi.loan.business.service.family.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.family.ActivityModel;
import com.tuanzi.loan.business.entity.family.ModuleContentItem;
import com.tuanzi.loan.business.repository.family.ActivityModelRepository;
import com.tuanzi.loan.business.service.family.ActivityModelService;
import com.tuanzi.loan.business.service.family.ModuleContentItemService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.generated.jooq.loan.tables.records.ActivityModelRecord;
import com.tuanzi.loan.vo.request.family.ActivityModelSearchRequest;
import com.tuanzi.loan.vo.response.family.ActivityModelSearchResponse;
import com.tuanzi.loan.vo.view.family.ActivityModelView;
import com.tuanzi.loan.vo.view.family.ActivityModelView.ContentItem;

@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class ActivityModelServiceImpl implements ActivityModelService {

	@Autowired
	private ModuleContentItemService moduleContentItemService;

	@Autowired
	private ActivityModelRepository activityModelRepository;

	@Override
	public Page<ActivityModelSearchResponse> list(PageSearcher<ActivityModelSearchRequest> pageSearcher) {
		return activityModelRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
	}

	@Override
	public ActivityModelView findViewById(Long id) {
		ActivityModelView view = new ActivityModelView();
		ActivityModel model = activityModelRepository.findOneById(id);
		if (null == model) {
			return view;
		}

		view.setId(model.getId());
		view.setModelName(model.getModelName());
		view.setModelRemark(model.getModelRemark());
		view.setModelType(model.getModelType());
		view.setTargetUrl(model.getTargetUrl());

		List<ModuleContentItem> moduleContentItems = moduleContentItemService.findAllByModelId(model.getId());
		if (null == moduleContentItems || moduleContentItems.isEmpty()) {
			view.setItems(Lists.newArrayList());
			return view;
		}

		view.setItems(moduleContentItems.stream().map(i -> ContentItem.builder().id(i.getId()).name(i.getName()).content(i.getContent()).remark(i.getRemark()).sort(i.getSort())
				.type(i.getType()).modelId(i.getModelId()).build()).collect(Collectors.toList()));
		return view;
	}

	@Override
	public ActivityModelView saveActivityModelView(ActivityModelView view) {
		ActivityModelRecord record = activityModelRepository.saveOrUpdate(view);

		moduleContentItemService.deleteByModelId(record.getid());
		if (!view.getItems().isEmpty()) {
			for (int i = 0; i < view.getItems().size(); i++) {
				ContentItem item = view.getItems().get(i);
				item.setSort(i);
				moduleContentItemService.save(record.getid(), item);
			}
		}

		return findViewById(record.getid());
	}

	@Override
	public List<ActivityModel> findAll() {
		return activityModelRepository.findAllActivityModel();
	}

	@Override
	public void deleteModel(Long id) {
		activityModelRepository.deleteModel(id);
		moduleContentItemService.deleteByModelId(id);
	}

}
